﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Xml;
using FreshTexture.Enums;
using FreshTexture.Model;

namespace FreshTexture.Settings
{
    /// <summary>
    ///     Klasa obsługuje globalne ustawienia aplikacji.
    /// </summary>
    internal class MySettings
    {
        private static String appName;
        protected static Window MainWindow { get; set; }

        public void InitializeSettings()
        {
            StaticLists.BlocksName = new List<string>();
            appName = Properties.Settings.Default.AppName;
            Properties.Settings.Default.Reload();
            Debug.WriteLine("Program Settings Loaded !");
            

            SetDirsPaths();
            SetBlocksFromXML();
            Properties.Settings.Default.Save();
            Debug.WriteLine("Program Settings Saved !");
        }

        private static void SetBlocksFromXML()
        {
            string pathToXml =
                @"C:\Users\Wedlock\Documents\Visual Studio 2012\Projects\FreshTexture\FreshTexture\blocks.xml";


            var xDoc = new XmlDocument();
            xDoc.Load(pathToXml);
            XmlElement dd = xDoc.DocumentElement;
            XmlNodeList nodes = dd.ChildNodes;

            SetSettings(nodes.Count, MyEnums.PropertyName.MaxKnownEntities);

            for (int i = 0; i < nodes.Count; i++)
            {
                StaticLists.BlocksName.Add(nodes.Item(i).Name);
            }
        }

        private static void SetDirsPaths()
        {
            SetSettings(Path.GetFullPath(Assembly.GetExecutingAssembly().Location), MyEnums.DirsKeyWords.Current);
            SetSettings(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                        MyEnums.DirsKeyWords.Appdata);

            string combined = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                           ".minecraft");
            SetSettings(combined, MyEnums.DirsKeyWords.Minecraft);

            combined = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), appName,
                                    "Cache");
            SetSettings(combined, MyEnums.DirsKeyWords.Cache);
        }

        private static void SetSettings(string propertyValue, Enum propertyName)
        {
            if (propertyValue != null)
            {
                Properties.Settings.Default[propertyName.ToString()] = propertyValue;
                Debug.WriteLine("Property {0} set to : {1}", propertyName, propertyValue);
            }
        }

        private static void SetSettings(int propertyValue, Enum propertyName)
        {
            if (propertyValue != null)
            {
                Properties.Settings.Default[propertyName.ToString()] = propertyValue;
                Debug.WriteLine("Property {0} set to : {1}", propertyName, propertyValue);
            }
        }

        public static string GetString(Enum propertyName)
        {
            return (string) Properties.Settings.Default[propertyName.ToString()];
        }

        public static string GetString(string propertyName)
        {
            return (string) Properties.Settings.Default[propertyName];
        }

        public static int GetInt(string propertyName)
        {
            return (int) Properties.Settings.Default[propertyName];
        }

        public static void SetMainWindow(Window window)
        {
            MainWindow = window;
        }

        public static Window GetMainWindow()
        {
            return MainWindow;
        }
    }
}